package com.douziit.meilianoa.base.utils;


import com.douziit.meilianoa.base.exception.TipException;

import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017/11/13
 */
public class StrUtil {

	private static Pattern PHONE_PATTERN = Pattern
			.compile("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$");

	/**
	 * 字符串为 null 或者内部字符全部为 ' ' '\t' '\n' '\r' 这四类字符时返回 true
	 */
	public static boolean isBlank(String str) {
		if (str == null) {
			return true;
		}
		int len = str.length();
		if (len == 0) {
			return true;
		}
		for (int i = 0; i < len; i++) {
			switch (str.charAt(i)) {
			case ' ':
			case '\t':
			case '\n':
			case '\r':
				// case '\b':
				// case '\f':
				break;
			default:
				return false;
			}
		}
		return true;
	}

	public static boolean notBlank(String str) {
		return !isBlank(str);
	}

	public static boolean notBlank(String... strings) {
		if (strings == null) {
			return false;
		}
		for (String str : strings) {
			if (isBlank(str)) {
				return false;
			}
		}
		return true;
	}

	public static boolean notNull(Object... paras) {
		if (paras == null) {
			return false;
		}
		for (Object obj : paras) {
			if (obj == null) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 生成UUID
	 *
	 * @return
	 */
	public static String randomUUID() {
		return UUID.randomUUID().toString();
	}

	/**
	 * 随机生成13位UUID
	 * 
	 * @return
	 */
	public static String random13UUID() {
		String uuid = UUID.randomUUID().toString().replace("-", "");
		String substring = uuid.substring(0, 13);
		return substring;
	}

	/**
	 * 随机生成32位UUID
	 *
	 * @return
	 */
	public static String random32UUID() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	/**
	 * 字符串数组中加入某个字符
	 *
	 * @param stringArray
	 *            数组
	 * @param separator
	 *            插入字符
	 * @return
	 */
	public static String join(Object[] stringArray, String separator) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < stringArray.length; i++) {
			if (i > 0) {
				sb.append(separator);
			}
			sb.append(stringArray[i]);
		}
		return sb.toString();
	}

	/**
	 * 判断字符是否满足分割
	 *
	 * @param str
	 *            字符串
	 * @param separator
	 *            分割符
	 * @return
	 */
	public static boolean check(String str, String separator) {
		if (str == null || str.length() < 1 || str.indexOf(separator) <= 0
				|| str.indexOf(separator) > str.length() - 2) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 判断是否传是否存在某个后缀, 不存在则加入
	 *
	 * @param str
	 *            字符串
	 * @param suffix
	 *            后缀
	 * @return
	 */
	public static String joinLastSuffix(String str, String suffix) {
		if (str == null) {
			return null;
		}
		int num = str.lastIndexOf(suffix);
		if (num + 1 == str.length()) {
			return str;
		} else {
			return str.concat(suffix);
		}
	}

	/***
	 * 生成订单编号
	 * 
	 * @param uid
	 * @return
	 */
	public static String getOrderNo(Integer uid) {
		StringBuilder sb = new StringBuilder();
		sb.append(DateUtil.toStrDate(new Date(), "yyyyMMddHHmmss"));
		Random random = new Random();
		sb.append(uid);
		for (int i = 0; i < 6 - uid.toString().length(); i++) {
			sb.append(random.nextInt(10));
		}
		return sb.toString();
	}

	public static String getStr32(String head) {

		StringBuilder sb = new StringBuilder();
		if (head == null) {
			head = "ULJ";
		}
		sb.append(head).append(DateUtil.toStrDate(new Date(), "yyyyMMddHHmmss"));
		Random random = new Random();
		int length = 18 - head.length();
		for (int i = 0; i < length; i++) {
			sb.append(random.nextInt(10));
		}
		return sb.toString();
	}

	/**
	 * 验证是否为手机号码
	 *
	 * @param phone
	 * @return true:是，false：不是
	 */
	public static boolean checkPhone(String phone) {
		if (phone == null) {
			return false;
		}
		Matcher m = PHONE_PATTERN.matcher(phone);
		if (!m.matches()) {
			throw new TipException("请输入正确的手机号");
		}
		return true;
	}

}
